package com.duoduo.admin.entity;

import java.io.Serializable;
import java.util.Objects;

import org.hibernate.Hibernate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Entity
@Table(name = "sys_user_post")
@IdClass(SysUserPost.UserPostId.class)
@Getter
@Setter
@ToString
public class SysUserPost {
	@Id
	@Column(name = "user_id", nullable = false)
	private Long userId;

	@Id
	@Column(name = "post_id", nullable = false)
	private Long postId;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
	@ToString.Exclude
	private SysUser user;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "post_id", nullable = false, insertable = false, updatable = false)
	@ToString.Exclude
	private SysPost Post;

	@Data
	public static class UserPostId implements Serializable {
		private static final long serialVersionUID = 2687087097095274803L;
		private Long userId;

		private Long postId;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o))
			return false;
		SysUserPost userPost = (SysUserPost) o;
		return userId != null && Objects.equals(userId, userPost.userId) && postId != null
				&& Objects.equals(postId, userPost.postId);
	}

	@Override
	public int hashCode() {
		return Objects.hash(userId, postId);
	}
}
